﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using Manager;
using Model;
using System.Data;
namespace HealthSystem.ReportPage
{
    public partial class SumReport : BasePage
    {
        HW_DistrictManager districtManager = new HW_DistrictManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                year.DataSource = ReportCofig.GetReportYear();
                year.DataTextField = "YearName";
                year.DataValueField = "YearCode";
                year.DataBind();
                string yearcode = null;
                if (Request["yearcode"] != null)
                {
                    yearcode = Request["yearcode"].ToString();
                    this.year.SelectedValue = yearcode;
                }
                BindGrid();
            }
        }
        private void BindGrid()
        {
            string yearcode = this.year.SelectedValue;
            DataTable list = new DataTable();
            if(Request["districtcode"]==null)
                list = districtManager.GetCityCount(CustomerProfile.DistrictId, yearcode);
            else
                list = districtManager.GetDisCount(Request["districtcode"].ToString(), yearcode);
           //DataTable list= districtManager.GetDisCount(CustomerProfile.DistrictId);
           Grid1.DataSource = list;
           Grid1.DataBind();

        }
        protected void Grid1_DoubleClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            string districtcode = Grid1.Rows[e.RowIndex].DataKeys[0] == null ? "" : Grid1.Rows[e.RowIndex].DataKeys[0].ToString();

            HW_DistrictManager districtManager = new HW_DistrictManager();
            
            string name = Grid1.Rows[e.RowIndex].DataKeys[1] == null ? "" : Grid1.Rows[e.RowIndex].DataKeys[1].ToString();
            //string level = Grid1.Rows[e.RowIndex].DataKeys[1].ToString();
            if (!string.IsNullOrEmpty(districtcode))
            {
                var district= districtManager.GetById(districtcode);
                string yearcode = year.SelectedValue;
                if (district.Level == 3)
                {
                    PageContext.RegisterStartupScript(Window1.GetShowReference("sumreport.aspx?districtcode=" + districtcode + "&yearcode=" + yearcode, name));
                }
                else if(district.Level==4)
                {
                    PageContext.RegisterStartupScript(Window1.GetShowReference("~/MySchool.aspx?districtid=" + districtcode + "&yearcode=" + yearcode, name));
                }

            }
        }
        protected void Year_Changed(object sender,EventArgs e)
        {
            BindGrid();
        }
    }
}